import { message } from "antd";
import jsonp from "jsonp";
import { WEATHER_APPID, WEATHER_APPSECRET } from "../config";
import axiosInstance from "./axiosInstance";

/**
 * 登录
 * @param {用户名} username 
 * @param {密码} password 
 * @returns 返回带着用户信息的promise实例
 */
export const reqLogin = (username, password) => axiosInstance.post("/login", { username, password });

/**
 * 根据id获取商品分类
 * @param {分类id} categoryId 
 * @returns 返回带着商品分类的promise实例
 */
export const reqCategoryById = (categoryId) => axiosInstance.get("/manage/category/info", { params: { categoryId } });

/**
 * 获取商品分类列表
 * @returns 返回带着商品分类列表的promise实例
 */
export const reqCategoryList = () => axiosInstance.get("/manage/category/list");

/**
 * 增加商品分类
 * @param {分类名} categoryName 
 * @param {父分类Id} parentId 
 * @returns 返回带着新增后分类对象的promise实例
 */
export const reqCategoryAdd = (categoryName, parentId = "0") => axiosInstance.post("/manage/category/add", { categoryName, parentId });

/**
 * 修改商品分类
 * @param {分类id} categoryId 
 * @param {分类名} categoryName 
 * @returns 返回带着更改后分类对象的promise实例
 */
export const reqCategoryUpdate = (categoryId, categoryName) => axiosInstance.post("/manage/category/update", { categoryName, categoryId });

/**
 * 获取商品列表
 * @param {页码} pageNum 
 * @param {每页的数量} pageSize 
 * @returns 返回带着商品列表的promise实例
 */
export const reqProductList = (pageNum, pageSize) => axiosInstance.get("/manage/product/list", { params: { pageNum, pageSize } });

/**
 * 搜索商品
 * @param {页码} pageNum 
 * @param {每页的数量} pageSize 
 * @param {商品名称或商品描述，productDesc、productName，与商品描述二选一} searchType 
 * @param {搜索的关键字} keyword 
 * @returns 返回带着商品列表的promise实例
 */
export const reqProductSearch = (pageNum, pageSize, searchType, keyword) => axiosInstance.get("/manage/product/search", { params: { pageNum, pageSize, [searchType]: keyword } });

/**
 * 根据商品id获取商品详情
 * @param {商品id} productId 
 * @returns 返回带着商品详情的promise实例
 */
export const reqProductInfoById = (productId) => axiosInstance.get("/manage/product/getProductInfo", { params: { productId } });

/**
 * 更新商品状态（上下架）
 * @param {商品id} productId 
 * @param {状态} status 
 * @returns 返回成功与否的promise实例
 */
export const reqProductUpdateStatus = (productId, status) => axiosInstance.post("/manage/product/updateStatus", { productId, status });


/**
 * 新增商品
 * @param {新增的商品对象} productObj 
 * @returns 返回成功与否的promise实例 
 */
export const reqProductAdd = (productObj) => axiosInstance.post("/manage/product/add", productObj);

/**
 * 更新商品
 * @param {更新的商品对象} productObj 
 * @returns 返回成功与否的promise实例
 */
export const reqProductUpdate = (productObj) => axiosInstance.post("/manage/product/update", productObj);

/**
 * 删除图片
 * @param {图片名称} name 
 * @returns 
 */
export const reqDeleteProductImg = (name) => axiosInstance.post("/manage/img/delete", { name });

/**
 * 获取角色列表
 * @returns 返回包含角色列表的promise实例
 */
export const reqRoleList = () => axiosInstance.get("/manage/role/list");

/**
 * 新增角色
 * @param {角色名称} roleName 
 * @returns 返回包含新增的角色对象的promise实例 
 */
export const reqRoleAdd = (roleName) => axiosInstance.post("/manage/role/add", roleName);

/**
 * 给角色授权
 * @param {角色id} _id 
 * @param {可操作的权限菜单数组} menus 
 * @param {授权时间} auth_time 
 * @param {授权人名称} auth_name 
 * @returns 返回包含角色对象的promise实例
 */
export const reqRoleUpdate = (_id, menus, auth_name) => axiosInstance.post("/manage/role/update", { _id, menus, auth_time: Date.now(), auth_name });

/**
 * 获取用户列表
 * @returns 返回包含用户和角色数组的promise实例
 */
export const reqUserList = () => axiosInstance.get("/manage/user/list");

/**
 * 新增用户
 * @param {新增用户对象} userObj 
 * @returns 返回包含新增用户对象的promise实例
 */
export const reqUserAdd = (userObj) => axiosInstance.post("/manage/user/add", userObj);

/**
 * 更新用户
 * @param {更新用户对象}} userObj 
 * @returns 返回包含更新后用户对象的promise实例
 */
export const reqUserUpdate = (userObj) => axiosInstance.post("/manage/user/update",userObj);

/**
 * 更改用户密码
 * @param {用户id} userId 
 * @param {新密码} password 
 * @returns 返回包含更改密码后用户对象的promise实例
 */
export const reqUserUpdatePassword = (userId,password) => axiosInstance.post("/manage/user/updatePassword",{userId,password});

/**
 * 删除用户
 * @param {用户id} userId 
 * @returns 返回包含成功与否字符串的promise实例
 */
export const reqUserDelete = (userId) => axiosInstance.post("/manage/user/delete",{userId});

/**
 * 获取天气
 * @returns 返回带着天气信息的promise实例
 */
export const reqWeather = () => {
    return new Promise((resolve, reject) => {
        jsonp(`https://www.tianqiapi.com/api?version=v1&appid=${WEATHER_APPID}&appsecret=${WEATHER_APPSECRET}&city=%E5%AE%81%E6%B3%A2&vue=1`, (error, data) => {
            if (error) {
                message.error("请求天气接口失败,请联系管理员");
                return new Promise(() => { });
            } else {
                const { tem, wea, wea_img } = data.data[0];
                resolve({ tem, wea, wea_img });
            }
        })
    });
}